{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 网络节点stop\n",
    "tensorflow中有一个计算梯度的函数tf.gradients(ys, xs)，要注意的是，xs中的x必须要与ys相关，不相关的话，会报错。  \n",
    "代码中定义了两个变量w1， w2， 但res只与w1相关  \n",
    "可见，一个节点被 stop之后，这个节点上的梯度，就无法再向前BP了。由于w1变量的梯度只能来自a节点，所以，计算梯度返回的是None。  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[None, <tf.Tensor 'gradients/Mul_1_grad/Reshape_1:0' shape=() dtype=float32>]\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf  \n",
    "  \n",
    "w1 = tf.Variable(2.0)  \n",
    "w2 = tf.Variable(2.0)  \n",
    "  \n",
    "a = tf.multiply(w1, 3.0)  \n",
    "a_stoped = tf.stop_gradient(a)  \n",
    "  \n",
    "# b=w1*3.0*w2  \n",
    "b = tf.multiply(a_stoped, w2)  \n",
    "gradients = tf.gradients(b, xs=[w1, w2])  \n",
    "print(gradients)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tensor 并不保存值，只是对结果的引用\n",
    "保存了3个属性：name, shape, type  \n",
    "add:0 表示为该节点的第一个输出  \n",
    "shape 表示输出为1维数组，长度为4  \n",
    "type 变量类型在创建之后不可改变  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"add_1:0\", shape=(4,), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf \n",
    "a = tf.constant([1, 2, 4 ,5], name = \"a\")\n",
    "b = tf.constant([2, 3, 4 ,5], name = \"b\")\n",
    "result = tf.add(a,b,name = \"add\")\n",
    "print (result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tensor的取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3  5  8 10]\n"
     ]
    }
   ],
   "source": [
    "init = tf.global_variables_initializer()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    #sess.run(result)\n",
    "    print (result.eval())\n",
    "    #print (sess.run(result))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## tf.InteractiveSession 直接将会话注册为默认会话"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3  5  8 10]\n"
     ]
    }
   ],
   "source": [
    "sess = tf.InteractiveSession()\n",
    "print (result.eval())\n",
    "sess.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-15.35807  -15.35807  -15.35807 ]\n",
      " [ 62.615593  62.615593  62.615593]\n",
      " [-41.004803 -41.004803 -41.004803]\n",
      " [ -9.809562  -9.809562  -9.809562]\n",
      " [ 22.583532  22.583532  22.583532]]\n"
     ]
    }
   ],
   "source": [
    "X = tf.Variable(tf.truncated_normal([5,3],stddev=2))\n",
    "W = tf.Variable(tf.fill([3,3],9.0),dtype = tf.float32)\n",
    "h = tf.matmul(X,W)\n",
    "init = tf.global_variables_initializer()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    print (h.eval())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'Variable_10:0' shape=(5, 3) dtype=float32_ref>\n"
     ]
    }
   ],
   "source": [
    "print (X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"a_2:0\", shape=(4,), dtype=int32)\n",
      "Tensor(\"b_2:0\", shape=(1, 4), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "a = tf.constant([1, 2, 4 ,5], name = \"a\")\n",
    "print (a)\n",
    "b = tf.constant([[1, 2, 4 ,5]], name = \"b\")\n",
    "print (b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
